Skip to content

feat(core): the preview can stream an element's geometry to the editor every frame - #8052

Merged
miguel-heygen merged 7 commits into
mainfrom
feat/editing-port-watch-geometry
Nov 17, 2026
Merged

miguel-heygen merged 7 commits into
mainfrom
feat/editing-port-watch-geometry

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

What

Adds a new editing-port request, watch-geometry. The editor names up to 32 element handles. On every animation frame, the preview then pushes those elements' geometry to the editor, with the same fields the geometry reply already gives, stamped with performance.timeOrigin + performance.now().

The push stops when:

  • the editor sends an empty list,
  • the page hides,
  • the editor disposes its port client.

The editor's client parses every push against a field allowlist and drops anything else.

Why

The edit-accuracy bench checks that a dragged element stays under the pointer. To do that it needs the element's position and the pointer's position at the same moment.

  • When the bench can read the preview's document directly, it reads both in one paint.
  • A preview the editor cannot script can only be measured by asking over the port. Each reply then lands a few frames after the paint it describes, so the bench pairs a box with a later pointer. This scores as jumps that never happened. On every sequence case that was 5 of 73.

With a pushed, timestamped stream, the bench can pair each frame's geometry with the pointer's position at that same time. That is the same pairing the direct read gets.

Related work

Refs #8010. That bench change will use this request, and will land after this PR.

How

  • packages/core/src/runtime/editing/watchGeometry.ts holds the new kind:
    • The params parser takes up to MAX_WATCHED_GEOMETRY (32) handles, and an empty list as the stop.
    • The frame side measures in its ResizeObserver step, which runs after every rAF callback and layout, just before paint. A private observer watches document.documentElement. Each frame, a rAF callback calls observe(), which resets the last reported size, so the root reports once whether or not anything resized. The callback then measures, pushes and calls unobserve(). No node is added to the page. A measure taken inside rAF, or in a task after it, could report a state that a later callback or task changed before paint. It measures with elementGeometry, the function that answers geometry, so a push carries exactly the fields a reply does, and nothing new.
    • At most MAX_GEOMETRY_WATCHERS (8) ports may watch at once. A further port's request fails, and a port that is already watching can still change its list.
    • A port whose postMessage throws loses its watch. A measure that throws is logged through swallow, and the next frame is still scheduled (finally).
    • geometryEvent is the editor-side parser. It checks every key at every level (rect, corners, source, clip path, members, visible) and drops a push with any unknown field, wrong type or over-cap list. It works the way keyEvent does for watch-keys.
  • editingPort.ts has the kind's entry in the request list, its types and its parser. The client gains onGeometry(listener). If the client ever asked for watch-geometry, dispose() first sends the empty list, so the frame stops pushing to a released port.
  • editingAgent.ts adds the handler, and its dispose() stops every geometry watch.
  • The two clocks are performance.timeOrigin + performance.now() on each side. I measured them in Chromium 152 headless, with the frame on another origin in its own process (--site-per-process), over 3 runs of 300 round trips each:
    • The frame's stamp sits between the parent's send and receive stamps every time.
    • The median offset is 0.05 ms, which is the timer's resolution. The largest |offset| was 3.2 ms, in a round trip that took 8 ms, so it is bounded by the round trip and not by the clocks.

Test plan

  • Unit tests added/updated:
    • editing/watchGeometry.test.ts:
      • The parser takes up to the cap and refuses an over-cap list, a non-handle and an extra field.
      • geometryEvent takes what the frame measures. It drops a push with an extra field at the top level, inside an element, inside a rect or member rect, a wrong version or event name, a non-finite stamp, a short corners or source, and a too-long clip path.
      • With stubbed animation frames, the frame pushes each frame with the shared-clock stamp and null for a gone handle. It stops on an empty list, on pagehide, and on the agent's stop.
    • editingPort.test.ts: the client hands valid pushes to its listeners, drops an off-shape one, and sends the empty list when disposed.
    • editingAgent.test.ts: watch-geometry refuses bad params like every other kind, and is listed in hello.
    • Core's three editing-port test files pass 174 of 174, three runs in a row. Core's typecheck, oxfmt and oxlint are clean.
    • fails without the fix: editing/watchGeometry.test.ts and editingPort.test.ts. Each test is shown to fail with its guard removed:
      • keysAre dropped from the element check: 1 test fails.
      • The cap check removed: 2 tests fail.
      • The stop on dispose removed: 1 test fails.
      • The pagehide listener removed: 1 test fails.
      • The watcher cap removed: 1 test fails.
      • The drop on a throwing port removed: 1 test fails.
      • The finally removed: 3 tests fail.
      • The measure moved back inside the rAF callback: 2 tests fail.
      • The observer left attached on a stop: 1 test fails.
  • Manual testing performed: not in a browser editor. The clock measurement above ran in Chromium. The bench change that uses this request will show the end-to-end result: 73 of 73 sequence cases both sealed and unsealed.
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

@miguel-heygen
miguel-heygen force-pushed the feat/editing-port-watch-geometry branch from f13a914 to db4a0e5 Compare November 17, 2026 07:16
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 99 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at b09a92d.

  • One watch per port: each new request replaces the port's watch, and parseWatchGeometry caps it at 32 handles.
  • Teardown: dispose() sends an empty list before closing the port. pagehide and agent dispose clear every watch, and the rAF loop cancels when none remain.
  • Rate: pushes are coalesced to one per frame by that single rAF loop.
  • Exposure: pushes reuse elementGeometry on handles that pass EDITING_ELEMENT_HANDLE, so they expose nothing beyond a geometry reply.
  • Test: the fails without the fix: line is present.

Should-fix:

  1. Port count: installEditingAgent accepts any number of editing-connect ports, each with up to 32 handles. Cap the total number of watchers or ports.
  2. Lost ports: a port lost without dispose() keeps its watch and is measured every frame until pagehide. Drop a watch whose postMessage throws.
  3. Loop stops on error: push() calls schedule() last, so an exception from elementGeometry stops every watch. Use try/finally.
  4. Client bounds: geometryEvent only checks that values are finite, with no MAX bounds and no watch id, so pushes still in flight after a stop reach listeners. The consumer (the #8010 bench) must clamp and drop stale pushes before anything is persisted, as on #8002 and #8005.

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Done in b729f735. The first three are in this PR, and the fourth is in the bench (#8010):

  1. Port count: at most MAX_GEOMETRY_WATCHERS (8) ports may watch at once. A ninth port's watch-geometry request fails with a typed failed reply, and a port that already watches can still change its list. Test: refuses a port past the watcher cap, and still lets a watching port change its list.
  2. Lost ports: a port whose postMessage throws loses its watch, and the other ports keep their pushes. Test: drops a watch whose port throws, and keeps pushing to the rest.
  3. Loop on error: each frame's pushes run in try/finally, so the next frame is scheduled even if a measure throws. Test: keeps the loop running when a frame's measure throws.
  4. Stale pushes: the bench drops every push that arrives between a re-watch and that request's reply. Port messages arrive in order, so anything before the reply belongs to the previous watch. It also drops any push stamped before the drag's start and any push with an element that isn't finite. That lands with fix(studio): the edit bench reads a sealed preview through its editing port #8010 on top of this PR.

Each new test fails with its guard removed. Core's editing-port tests pass 173 of 173, three runs in a row; typecheck, oxfmt, oxlint and fallow are clean.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at b729f73. This covers three of my four should-fixes:

  • Watcher cap: MAX_GEOMETRY_WATCHERS = 8 ports per page. A new port past the cap is refused, and an existing one can still re-watch.
  • Lost ports: pushTo drops a watch whose postMessage throws.
  • Errors: schedule() now runs in finally, so one failed measure doesn't stop every watch.

Still open: the client-side bounds, for the #8010 consumer.

@miguel-heygen
miguel-heygen force-pushed the feat/editing-port-watch-geometry branch from b729f73 to 813c845 Compare November 17, 2026 08:13

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at 813c845. This is a rebase onto main: the range-diff against b729f73 shows commits 2 and 3 identical, and commit 1 differs only in context.

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 17, 2026

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at 83708b1. Watched geometry is now measured in a MessageChannel task posted from rAF, so after the frame renders rather than inside rAF, where a later callback could still change what paints. A throwing push is caught with swallow, and schedule() stays in finally, so the loop survives.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at c856c25. The measurement now runs in the frame's ResizeObserver step, after every rAF callback and layout and just before paint, rather than in a task after the frame.

  • Loop: rAF observes documentElement. A fresh observe() reports a non-zero root once, push unobserves, and schedule in finally re-arms for the next frame.
  • Stopping: cancels the rAF and unobserves. A new test covers a stop between the re-arm and the observer step.

Note: in a zero-size frame (a hidden iframe) the observer has nothing to report, so pushes pause until the frame gets a size, and then resume on their own. That's fine for a frame with no geometry, but worth a comment.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 5047d4f8. I found no blocker.

Delta since the last approved head (c856c25c). It is two commits, and both change comments only. watchGeometry.ts:105 gains the hidden-frame note, and the fake ResizeObserver comment is reworded. Since this was my first review of the PR, I also reviewed all of it.

Timing. The rAF callback calls observe(), and the measure runs in the ResizeObserver step, after all rAF callbacks and layout and before paint. Because observe() resets the last reported size, the root reports once per frame. push reschedules in finally, and the last stop cancels the pending rAF and detaches the observer, including when a stop lands between the rAF and the ResizeObserver step. A 0x0 hidden frame pauses and resumes on its own once it has a size.

Bounds. Pushes reuse elementGeometry, the same code the geometry reply uses, so no new data leaves the frame. The caps hold: at most 32 handles per watch and 8 watching ports. geometryEvent checks the exact key set at every level, requires finite numbers and caps list lengths. Dispose sends the empty list before it closes the port.

Fails without the fix. The named tests pass, 174/174, three runs in a row. Each mutation fails the number of tests the body predicts:

  • Measuring back inside rAF fails 2.
  • Leaving the observer attached on stop fails 1.
  • Sending no stop message on dispose fails 1.
  • Removing the watcher cap fails 1.
  • Using isRecord instead of keysAre fails 1.

Nit (non-blocking). Removing the unobserve at the top of push passes every test. In a real browser that's harmless, because the next observe() resets the size anyway.

CI. Every check-run at this head is green or skipped.

— Rames

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 17, 2026
Merged via the queue into main with commit f9c5ed0 Nov 17, 2026
98 checks passed
@miguel-heygen
miguel-heygen deleted the feat/editing-port-watch-geometry branch November 17, 2026 12:53
This was referenced Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants